package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.InGoodsAnalysis;
import pers.cj.pdos.mapper.InGoodsAnalysisMapper;
import pers.cj.pdos.service.IInGoodsAnalysisService;
import pers.cj.pdos.utils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Service
public class InGoodsAnalysisServiceImpl extends ServiceImpl<InGoodsAnalysisMapper, InGoodsAnalysis> implements IInGoodsAnalysisService {
    @Autowired
    InGoodsAnalysisMapper inGoodsAnalysisMapper;

    @Override
    public Map getTotalData(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getTotalData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<InGoodsAnalysis> listStoreData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.listStoreData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public List<InGoodsAnalysis> listHouseData(String companyId, Integer page, Integer limit, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.listHouseData(companyId, Utils.genPage(page,limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public List<InGoodsAnalysis> listGoodsData(String companyId,
                                                    Integer page,
                                                    Integer limit,
                                                    String storeId,
                                                    String houseId,
                                                    String providerId,
                                                    Date date1,
                                                    Date date2,
                                                    String typeId,
                                                    Boolean filterZero,
                                                    String search) {
        return inGoodsAnalysisMapper.listGoodsData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, providerId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public Long countGoodsData(String companyId,
                               String storeId,
                               String houseId,
                               String providerId,
                               Date date1,
                               Date date2,
                               String typeId,
                               Boolean filterZero,
                               String search) {
        return inGoodsAnalysisMapper.countGoodsData(companyId, storeId, houseId, providerId, date1, date2, typeId, filterZero, search);
    }

    @Override
    public List<InGoodsAnalysis> listProviderData(String companyId,
                                                          Integer page,
                                                          Integer limit,
                                                          String storeId,
                                                          String houseId,
                                                          Date date1,
                                                          Date date2,
                                                          String providerTypeId,
                                                          Boolean filterZero,
                                                          String search) {
        List<InGoodsAnalysis> list = inGoodsAnalysisMapper.listProviderData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2, providerTypeId, filterZero, search);

        BigDecimal total = inGoodsAnalysisMapper.getTotalProvider(companyId, storeId, houseId, date1, date2, providerTypeId, filterZero, search);
        if (!Utils.isEmpty(list) && total.compareTo(new BigDecimal(0))!=0) {
            List<InGoodsAnalysis> newList = new ArrayList<>();
            for (InGoodsAnalysis entity:list) {
                entity.setRatio(entity.getAmount().multiply(new BigDecimal(100)).divide(total,4, RoundingMode.HALF_UP));
                newList.add(entity);
            }
            return newList;
        }

        return list;
    }

    @Override
    public Long countProviderData(String companyId,
                                  String storeId,
                                  String houseId,
                                  Date date1,
                                  Date date2,
                                  String providerTypeId,
                                  Boolean filterZero,
                                  String search) {
        return inGoodsAnalysisMapper.countProviderData(companyId, storeId, houseId, date1, date2, providerTypeId, filterZero, search);
    }

    @Override
    public BigDecimal getTotalProvider(String companyId,
                                       String storeId,
                                       String houseId,
                                       Date date1,
                                       Date date2,
                                       String providerTypeId,
                                       Boolean filterZero,
                                       String search) {
        return inGoodsAnalysisMapper.getTotalProvider(companyId, storeId, houseId, date1, date2, providerTypeId, filterZero, search);
    }

    @Override
    public List<InGoodsAnalysis> listOrderData(String companyId,
                                                    Integer page,
                                                    Integer limit,
                                                    String storeId,
                                                    String houseId,
                                                    Date date1,
                                                    Date date2) {
        return inGoodsAnalysisMapper.listOrderData(companyId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2);
    }

    @Override
    public Long countOrderData(String companyId,
                               String storeId,
                               String houseId,
                               Date date1,
                               Date date2) {
        return inGoodsAnalysisMapper.countOrderData(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighGoods(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getHighGoods(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public Map getHighProvider(String companyId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getHighProvider(companyId, storeId, houseId, date1, date2);
    }

    @Override
    public List<InGoodsAnalysis> listGoodsDetailDataByOrder(String companyId,
                                                                            String goodsId,
                                                                            Integer page,
                                                                            Integer limit,
                                                                            String storeId,
                                                                            String houseId,
                                                                            Date date1,
                                                                            Date date2,
                                                                            String search) {
        return inGoodsAnalysisMapper.listGoodsDetailDataByOrder(companyId, goodsId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2, search);
    }

    @Override
    public Long countGoodsDetailDataByOrder(String companyId,
                                            String goodsId,
                                            String storeId,
                                            String houseId,
                                            Date date1,
                                            Date date2,
                                            String search) {
        return inGoodsAnalysisMapper.countGoodsDetailDataByOrder(companyId, goodsId, storeId, houseId, date1, date2, search);
    }

    @Override
    public List<InGoodsAnalysis> listGoodsDetailDataByProvider(String companyId,
                                                                                  String goodsId,
                                                                                  Integer page,
                                                                                  Integer limit,
                                                                                  String storeId,
                                                                                  String houseId,
                                                                                  Date date1,
                                                                                  Date date2,
                                                                                  String search) {
        return inGoodsAnalysisMapper.listGoodsDetailDataByProvider(companyId, goodsId, Utils.genPage(page, limit), limit, storeId, houseId, date1, date2, search);
    }

    @Override
    public Long countGoodsDetailDataByProvider(String companyId,
                                               String goodsId,
                                               String storeId,
                                               String houseId,
                                               Date date1,
                                               Date date2,
                                               String search) {
        return inGoodsAnalysisMapper.countGoodsDetailDataByProvider(companyId, goodsId, storeId, houseId, date1, date2, search);
    }

    @Override
    public Map getMaxMinDate(String companyId, String goodsId, String providerId, String storeId, String houseId) {
        return inGoodsAnalysisMapper.getMaxMinDate(companyId, goodsId, providerId, storeId, houseId);
    }

    @Override
    public List<Map> getTotalByYear(String companyId, String goodsId, String providerId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getTotalByYear(companyId, goodsId, providerId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByMonth(String companyId, String goodsId, String providerId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getTotalByMonth(companyId, goodsId, providerId, storeId, houseId, date1, date2);
    }

    @Override
    public List<Map> getTotalByDate(String companyId, String goodsId, String providerId, String storeId, String houseId, Date date1, Date date2) {
        return inGoodsAnalysisMapper.getTotalByDate(companyId, goodsId, providerId, storeId, houseId, date1, date2);
    }
}
